package com.yh.servlet;

import com.yh.controller.AllController;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;

@WebServlet("/*")
public class AllServlet extends HttpServlet {
    AllController allController=new AllController();

    @Override
    public void init(ServletConfig config) throws ServletException {
        HashMap<String, String> map = allController.map;
        map.put("/springmvc0909_1/a","a");
        map.put("/springmvc0909_1/b","b");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        System.out.println("requestURI:"+requestURI);

        HashMap<String, String> map = allController.map;
        String methodName = map.get(requestURI);

        //通过反射调用对应方法
        Class<AllController> allControllerClass = AllController.class;
        try {
            Method declaredMethod = allControllerClass.getDeclaredMethod(methodName);
            //执行对应方法
            declaredMethod.invoke(allController);
        } catch (Exception e) {
        }
    }


}
